package ops.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ops.controller.ClassController;
import ops.controller.ControllerManager;
import ops.i18n.Resources;
import ops.util.ViewHelper;
import ops.view.forms.Form;
import ops.view.forms.component.ComboboxInput;
import ops.view.forms.component.NumberInput;
import ops.view.tables.Column;
import ops.view.tables.TableMaker;

public class ClassRegisterPanel extends JPanel implements Refreshable {

	private ClassController classController = ControllerManager.getInstance().getClassController();
	
	private JPanel formPanel;

	private JPanel buttonsPanel;
	private JButton newButton;
	private JButton saveButton;
	private JButton removeButton;
	private JButton cancelButton;

	private TableMaker tableMaker;
	private Form form;
	private ComboboxInput teachersList;
	private ComboboxInput subjectsList;
	private ComboboxInput termsList;

	public ClassRegisterPanel() {
		super(new BorderLayout());

		init();
	}

	public void clearTable() {
		tableMaker.clearTable();
	}
	
	public void addRowToTable(Object... data) {
		tableMaker.addRow(data);
	}

	private void init() {
	
		formPanel = new JPanel(new BorderLayout());

		initButtons();
		initForm();
		initTable();

		add(formPanel, BorderLayout.SOUTH);
	}
	
	private void initForm() {
		Resources resources = Resources.getInstance();
		
		form = new Form();
		
		teachersList = new ComboboxInput(resources.getString("class.teacher")+":", true, classController.getTeachersList());
		subjectsList = new ComboboxInput(resources.getString("class.subject")+":", true, classController.getSubjectsList());
		termsList = new ComboboxInput(resources.getString("class.term"), true, ControllerManager.getInstance().getTermController().getTermsList());
		
		form.addInput("class.code", new NumberInput(resources.getString("class.code")+":", 20, true, false));
		form.addInput("class.subject", subjectsList);
		form.addInput("class.size", new NumberInput(resources.getString("class.size")+":", 20, true));
		form.addInput("class.term", termsList);
		form.addInput("class.teacher", teachersList);
		
		formPanel.add(form.getPanel(), BorderLayout.SOUTH);
		
	}
	
	private void initTable() {
		Resources resources = Resources.getInstance();

		tableMaker = new TableMaker();
		tableMaker.addColumn(new Column(resources.getString("class.code"), Integer.class, 15));
		tableMaker.addColumn(new Column(resources.getString("class.subject"), String.class, 70));
		tableMaker.addColumn(new Column(resources.getString("class.size"), Integer.class, 3));
		tableMaker.addColumn(new Column(resources.getString("class.term"), String.class));
		tableMaker.addColumn(new Column(resources.getString("class.teacher"), String.class));
		tableMaker.setControlButtons(saveButton, removeButton);
		tableMaker.addSelectionListener(new SelectionListener());
		
		add(ViewHelper.makeScrollPane(tableMaker.getTable()), BorderLayout.CENTER);
	}
	
	private void initButtons() {
		buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));

		ButtonListener listener = new ButtonListener();

		removeButton = ViewHelper.makeRemoveButton();
		removeButton.addActionListener(listener);
		saveButton = ViewHelper.makeSaveButton();
		saveButton.addActionListener(listener);
		newButton = ViewHelper.makeNewButton();
		newButton.addActionListener(listener);
		cancelButton = ViewHelper.makeCancelButton();
		cancelButton.addActionListener(listener);
		
		buttonsPanel.add(newButton);
		buttonsPanel.add(saveButton);
		buttonsPanel.add(removeButton);
		buttonsPanel.add(cancelButton);

		formPanel.add(buttonsPanel, BorderLayout.CENTER);
	}
	
	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (!(e.getSource() instanceof JButton)) {
				return;
			}
			
			JButton button = (JButton) e.getSource();
			
			if (!button.isEnabled()) {
				return;
			}

			Integer code = form.getIntegerFor("class.code");
			Integer subject = null;
			try {
				subject = form.getIntegerFor("class.subject");
			} catch (RuntimeException e1) {
				Resources resources = Resources.getInstance();
				ViewHelper.showError(resources.getString("subject.missing"));
			}
			Integer size = form.getIntegerFor("class.size");
			Integer term = form.getIntegerFor("class.term");
			Long teacher = null;
			try {
				teacher = form.getLongFor("class.teacher");
			} catch (RuntimeException e1) {
				Resources resources = Resources.getInstance();
				ViewHelper.showError(resources.getString("teacher.missing"));
			}
			
			if (button == saveButton) {
				if (form.validate()) {				
					classController.saveClass(code, subject, size, term, teacher);
					
					saveButton.setEnabled(false);
					newButton.setEnabled(true);
					
					form.clear();
					classController.endAddProcess();
				} else {
					ViewHelper.showFormValidationError();
					return;
				}
			} else if (button == removeButton) {
				classController.removeClass(code);
			} else if (button == newButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(true);
				newButton.setEnabled(false);
				
				classController.startAddProcess();
			} else if (button == cancelButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(false);
				newButton.setEnabled(true);
				
				classController.endAddProcess();
			} 
		}
	}
	
	private class SelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			
			ListSelectionModel lsm = (ListSelectionModel) e.getSource();

			if (!lsm.isSelectionEmpty()) {
				int code = (Integer) tableMaker.getValueAt(tableMaker.getSeletedRow(), 0);
				String subject = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 1);
				int size = (Integer) tableMaker.getValueAt(tableMaker.getSeletedRow(), 2);
				String term = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 3);
				String teacher = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 4);
				
				form.setValue("class.code", code);
				form.setValue("class.subject", subject);
				form.setValue("class.size", size);
				form.setValue("class.term", term);
				form.setValue("class.teacher", teacher);
			} else {
				form.clear();
			}
		}		
	}

	public void refresh() {
		classController.updateTable();
		teachersList.updateData(classController.getTeachersList());
		subjectsList.updateData(classController.getSubjectsList());
		termsList.updateData(ControllerManager.getInstance().getTermController().getTermsList());
	}
}